package cn.scw.project.goods.controller;

import cn.scw.common.constant.SysConsts;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.goods.domain.netework.bean.GCommodityAddReq;
import cn.scw.project.goods.domain.netework.bean.GCommodityListReq;
import cn.scw.project.goods.domain.vo.GCommodityVo;
import cn.scw.project.goods.service.IGCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author api
 * @date 2020-05-20
 */
@Api(tags = "商品")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH+"/goods/commodity")
public class GCommodityController extends BaseController {
    @Autowired
    private IGCommodityService gCommodityService;


    @ApiOperation("查询商品列表")
    //@PreAuthorize("@ss.hasPermi('goods:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(GCommodityListReq req) {
        startPage();
        List<GCommodityVo> list = gCommodityService.selectGCommodityList(req);
        return getDataTable(list);
    }

    @ApiOperation("查询设备租赁列表-小程序")
    @GetMapping("/wx/rent/list/{purchasingTypes}")
    public TableDataInfo wxRentList(@PathVariable @ApiParam("商品类型（1设备租赁-包月，2设备租赁-两年赠机）") String purchasingTypes) {
        startPage();
        List<GCommodityVo> list = gCommodityService.selectGCommodityListByPurchasingTypes(purchasingTypes);
        return getDataTable(list);
    }

    @ApiOperation("根据设备详细类型查询设备采购和办公耗材-小程序")
    @GetMapping("/wx/type/list/{type}")
    public TableDataInfo wxList(@PathVariable @ApiParam("商品类型详细类型") String type) {
        startPage();
        List<GCommodityVo> list = gCommodityService.selectGCommodityListByType(type);
        return getDataTable(list);
    }

    @ApiOperation("获取商品详细信息")
    //@PreAuthorize("@ss.hasPermi('goods:commodity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gCommodityService.selectGCommodityVoById(id));
    }

    @ApiOperation("新增商品")
    //@PreAuthorize("@ss.hasPermi('goods:commodity:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GCommodityAddReq req) {
        return toAjax(gCommodityService.insertGCommodity(req));
    }

    @ApiOperation("修改商品")
    //@PreAuthorize("@ss.hasPermi('goods:commodity:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GCommodityAddReq req) {
        return toAjax(gCommodityService.updateGCommodity(req));
    }

    @ApiOperation("删除商品")
    //@PreAuthorize("@ss.hasPermi('goods:commodity:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gCommodityService.deleteGCommodityByIds(ids));
    }
}
